package net.wanho.zaixian.course.controller;


import com.alibaba.fastjson.JSONObject;
import net.wanho.zaixian.course.service.TeachPlanService;
import net.wanho.zaixian.pojo.course.Teachplan;
import net.wanho.zaixian.pojo.course.ext.TeachplanNode;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TeachPlanController {

    @Resource
    private TeachPlanService teachPlanService;

    /**
     * 查询某个课程的课程计划
     * @param courseId
     * @return
     */
    @GetMapping("/course/teachplan/list/{courseId}")
    public TeachplanNode getTeachPlanList(@PathVariable(name = "courseId")String courseId){
        System.out.println(courseId);
//        JSONObject jsonObject = new JSONObject();
        List<TeachplanNode> allTeachPlanByCourseId = teachPlanService.getAllTeachPlanByCourseId(courseId);
        System.out.println(allTeachPlanByCourseId);

        return allTeachPlanByCourseId.get(0);
    }


    /**
     * 添加课程
     * @param teachplan
     * @return
     */
    @PostMapping("/course/teachplan/add")
    public JSONObject addTeachPlan(@RequestBody Teachplan teachplan){
        JSONObject jsonObject = new JSONObject();
        teachplan.setGrade("3");

        try {
            teachPlanService.addTeachList(teachplan);
            jsonObject.put("success",true);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("success",false);
            return jsonObject;
        }

    }

    /**
     * 根据课程计划id查看课程计划
     * @param id
     * @return
     */
    @GetMapping("/course/teachplan/fetch/{id}")
    public Teachplan getTeachPlanById(@PathVariable(name = "id") String id){
        Teachplan teachPlanById = teachPlanService.getTeachPlanById(id);
        return teachPlanById;

    }

    /**
     * 根据课程计划id删除课程计划
     * @param id
     * @return
     */
    @DeleteMapping("/course/teachplan/delete/{id}")
    public JSONObject deleteTeachPlanById(@PathVariable(name="id") String id){
        JSONObject jsonObject = new JSONObject();

        try {
            teachPlanService.deleteTeachPlanById(id);
            jsonObject.put("success",true);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("success",false);
            return jsonObject;
        }
    }
}
